package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectQuestion;
import com.EM1.www.service.Checkquestion;
import com.EM1.www.util.UpdateSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/UpdateQuestion")
public class UpdateQuestion extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String question=request.getParameter("question");
        String answer=request.getParameter("answer");
        Checkquestion checkquestion=new Checkquestion();
        String warn = checkquestion.checkquestion(question, answer);
        if(warn!=null){
            request.setAttribute("warn",warn);
            request.setAttribute("question",question);
            request.setAttribute("answer",answer);
            request.getRequestDispatcher("addquestion.jsp").forward(request,response);
        }else {
            String parse=request.getParameter("parse");
            int id=(int)request.getSession().getAttribute("id");
            ConnectQuestion connectQuestion=new ConnectQuestion();
            try {
                connectQuestion.updateQuestion(question,answer,parse,id);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            try {
                UpdateSession.updateQuestion(request);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            request.getRequestDispatcher("questionbank.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
